#include "StdAfx.h"
#include "MonitorClient.h"
#include "BitmapEx.h"
#include "AppearanceServer.h"

CBitmapEx::CBitmapEx(void)
	: m_sizeBitmap(0, 0)
{
}

CBitmapEx::~CBitmapEx(void)
{
}

BOOL CBitmapEx::LoadBitmap(LPCTSTR lpszResource)
{
	HBITMAP hBitmap = theAppearanceServer.LoadBitmap(lpszResource);
	if(hBitmap == NULL)
	{
		hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_UNAVAILABLE));
	}
	return AttachBitmap(hBitmap);
}

BOOL CBitmapEx::LoadBitmapDirect(LPCTSTR lpszFilePath)
{
	HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, lpszFilePath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	return AttachBitmap(hBitmap);
}

BOOL CBitmapEx::CreateCompatibleBitmap(CDC* pDC, int nWidth, int nHeight)
{
	if(CBitmap::CreateCompatibleBitmap(pDC, nWidth, nHeight))
	{
		m_sizeBitmap.cx = nWidth;
		m_sizeBitmap.cy = nHeight;

		return TRUE;
	}
	else
	{
		return FALSE;	
	}
}

BOOL CBitmapEx::AttachBitmap(HGDIOBJ hBitmap)
{
	DeleteObject();

	if(hBitmap && Attach(hBitmap))
	{
		BITMAP bmp;
		GetBitmap(&bmp);

		m_sizeBitmap.cx = bmp.bmWidth;
		m_sizeBitmap.cy = bmp.bmHeight;

		return TRUE;
	}
	else
	{
		return FALSE;
	}
}